package org.example;

import com.google.gson.Gson;
import lx.mybatis.entity.BookInfo;
import lx.mybatis.mapper.DemoMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class demo {
    private static int num = 0;
    public static void main(String[] args) throws IOException {
        SqlSession sqlSession;
        String resource = "mybatis.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory =
                new SqlSessionFactoryBuilder().build(inputStream);
        sqlSession = sqlSessionFactory.openSession();
        DemoMapper mapper = sqlSession.getMapper(DemoMapper.class);
        findBookByPage(2, mapper);
        sqlSession.commit();
        sqlSession.close();


    }

    public static void findBookByPage(int page, DemoMapper mapper) throws IOException {
        String addr = "https://www.jiushujie.com/sell?ajax=yw0&page=" + page+ "&sort=created_at.desc&tag=recommend";
        URL url = new URL(addr);
        Document doc = Jsoup.parse(url, 5000);
        Elements elements = doc.select("div.book_pic>a");
        num += elements.size();
        for (Element element : elements) {
            String href = element.attr("href");
            String id = href.replaceAll("\\D", "");
            findBookBybid(id, mapper);
        }
        if (num <100) {
            System.out.println("/n" + num + "/n");
            findBookByPage(++page, mapper);

        }

    }
    public static void findBookBybid(String bid, DemoMapper mapper) throws IOException {
        String addr = "https://www.jiushujie.com/sell/" + bid;
        URL url = new URL(addr);
        BookInfo bookInfo = new BookInfo();
        Document doc = Jsoup.parse(url, 5000);
        bookInfo.setImg(doc.selectFirst("div.book_item div.book_pic>img").attr("src"));
        bookInfo.setName(doc.selectFirst("div.book_item div.info>h2").text());
        String detail = doc.selectFirst("div.book_item div.detail").text().trim();
        String[] details = detail.split("[\\s\\n/]+");
        bookInfo.setPress(details[0]);
        bookInfo.setAuthor(details[1]);
//        bookInfo.setIsbn(details[2]);
        bookInfo.setPrice(details[3].substring(1));
        bookInfo.setDisPrice(details[4].substring(1));
        bookInfo.setOld(details[5]);
        bookInfo.setCategory(details[6]);
        if(  doc.selectFirst("div.book_introduction_container") != null){
            bookInfo.setIntro(doc.selectFirst("div.book_introduction_container").html());
        }
        System.out.println("bookInfo: " + bookInfo);
        mapper.insertBook(bookInfo);


    }
    public static void main1(String[] args) throws IOException {
        String url = "https://www.vvic.com/apif/queryItemShopRecommended?city=gz";
        URL u = new URL(url);

        URLConnection urlConnection = u.openConnection();
        InputStream inputStream = urlConnection.getInputStream();
        InputStreamReader reader = new InputStreamReader(inputStream, "UTF-8");
        BufferedReader br = new BufferedReader(reader);
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = br.readLine()) != null){
            sb.append(line);
        }

        Gson gson = new Gson();
        HashMap hashMap = gson.fromJson(sb.toString(), HashMap.class);
        Map data =(Map)hashMap.get("data");
        List<Map> cityMarkets = (List<Map>)data.get("cityMarkets");
        for(int i = 0; i < cityMarkets.size(); i++){
            System.out.println(cityMarkets.get(i));
        }
    }


}
